package com.wx.disparent.mallparent.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 刘杰龙
 * @version 1.0
 * @description
 * @date 2019/12/14 9:56
 */
@Component
public class MyConfiguration implements WebMvcConfigurer, ErrorPageRegistrar {
    @Value("${file-path-pattern}")
    private String filePatten;
    @Value("${file-location}")
    private String filePath;

    /**
     * 虚拟路径配置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/"+filePatten+"/**").addResourceLocations("file:"+filePath+filePatten+ "/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        /*1、按错误的类型显示错误的网页*/
        /*错误类型为404，找不到网页的，默认显示404.html网页*/
        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/com/404");
        /*错误类型为500，表示服务器响应错误，默认显示500.html网页*/
        ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/com/404");
        ErrorPage e400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/com/404");
        registry.addErrorPages(e400 ,e404, e500);
    }
}